package com.seo.pojo;

import lombok.Data;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.transaction.interceptor.TransactionAspectSupport;


/**
 * 数据类型
 * 
 * @author shibeilei
 * @version 2017.12.13 11:42
 */
public class ReturnBody extends HashMap<String, Object> implements Serializable {
	private static final long serialVersionUID = 5075924626508128661L;

	public ReturnBody() {
		ResultInfo result = new ResultInfo();
		setResult(result);
	}

	public static ReturnBody init() {
		return new ReturnBody();
	}

	public static ReturnBody init(Map<String, Object> map) {
		ReturnBody body = init();
		body.putAll(map);
		return body;
	}

	@SuppressWarnings("unchecked")
	public <T> T get(String key, Class<T> type) {
		return (T) get(key);
	}

	@SuppressWarnings("unchecked")
	public <K, V> Map<K, V> get(String key, Class<K> keyType, Class<V> valueType) {
		return (Map<K, V>) get(key);
	}

	@SuppressWarnings("unchecked")
	public <T extends Collection<V>, V> T getCollection(String key, Class<V> valueType) {
		return (T) get(key);
	}

	public void run(ReturnBody sb) {
		putAll(sb);
		setResult(sb.getResult());
	}

	public ReturnBody success(String code, String message) {
		initResult(ResultInfo.SUCCESS, code, message);
		return this;
	}

	public ReturnBody clean() {
		ResultInfo result = getResult();
		clear();
		setResult(result);
		return this;
	}

	public ReturnBody success() {
		return success("ok", "成功");
	}

	public ReturnBody error(String code, String message) {
		initResult(ResultInfo.ERROR, code, message);
		return this;
	}

	public ReturnBody error() {
		return error("error", "网络繁忙");
	}

	public ReturnBody error(String message) {
		return error("error", message);
	}

	public ReturnBody rollback(String code, String message) {
		ResultInfo result = initResult(ResultInfo.ERROR, code, message);
		result.setIsRollBack(ResultInfo.ROLLBACK);
		return this;
	}

	public ReturnBody rollback() {
		return rollback("error", "网络繁忙");
	}

	public ReturnBody setRollback() {
		ResultInfo result = getResult();
		result.setIsRollBack(ResultInfo.ROLLBACK);
		return this;
	}
	
	public ReturnBody rollback(boolean isNew, boolean isClean) {
		if (isNew) {
			if (isClean) {
				clear();
			}
			return rollback("error", "网络繁忙");
		} else {
			ResultInfo result = getResult();
			result.setIsRollBack(ResultInfo.ROLLBACK);
			if (isClean) {
				clear();
			}
			setResult(result);
			return this;
		}
	}

	public ReturnBody rollback(String message) {
		return rollback("error", message);
	}

	public void result() {
		ResultInfo result = getResult();
		if (ResultInfo.ROLLBACK.equals(result.getIsRollBack())) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
	}

	public static void transactionRollback() {
		TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	}

	private ResultInfo initResult(String state, String code, String message) {
		ResultInfo result = getResult();
		clear(result);
		result.setState(state);
		result.setCode(code);
		result.setMessage(message);
		put("success", isSuccess());
		return result;
	}

	public boolean isSuccess() {
		ResultInfo result = getResult();
		return ResultInfo.SUCCESS.equals(result.getState());
	}

	private void clear(ResultInfo ri) {
		ri.setState(null);
		ri.setCode(null);
		ri.setMessage(null);
		ri.setUrl(null);
		ri.setIsRollBack("00");
		ri.setIsAgainLogin("00");
	}

	public ResultInfo getResult() {
		return (ResultInfo) get("result");
	}

	public void setResult(ResultInfo result) {
		put("result", result);
	}

	@Data
	public class ResultInfo implements Serializable {

		private static final long serialVersionUID = 1L;
		public static final String SUCCESS = "1";
		public static final String ERROR = "0";
		public static final String ROLLBACK = "01";

		private String state;
		private String code;
		private String Message;
		private String url;
		private String isRollBack;
		private String isAgainLogin = "00";

	}
}
